package com.dp.appi.entity;

import com.dp.appi.exception.CommonException;
import com.dp.appi.util.PasswordUtil;
import com.google.gson.Gson;
import org.apache.http.entity.StringEntity;

import javax.xml.crypto.Data;
import java.io.UnsupportedEncodingException;

/**
 * Created by xiaoji on 2016/4/20.
 */
public class UserEntity {
    public String a;//账号
    public String b;//手机号码
    public String c;//加密后的密码


    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getOpenIdent(){
        Gson gson = new Gson();
        String str = gson.toJson(this);
        String entity = null;
        try {
            entity = new String(str.getBytes("utf-8"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return PasswordUtil.encrypt(entity,"zerdoor",PasswordUtil.getStaticSalt());
    }

    public static UserEntity  decrypt(String openIdent) {
        if(openIdent==null||"".equals(openIdent)){
            throw new CommonException("EXPO014");
        }
        openIdent=PasswordUtil.decrypt(openIdent,"zerdoor",PasswordUtil.getStaticSalt());
        UserEntity userEntity = new Gson().fromJson(openIdent,UserEntity.class);
        return userEntity;
    }

    public static void main(String[] args) {
        UserEntity userEntity=new UserEntity();
        userEntity.setA("黄金发");
        userEntity.setB("15891061740");
        userEntity.setC("5fa9ae384e1ec5fd");
        System.out.println(userEntity.getOpenIdent());
        System.out.println(new Gson().toJson(decrypt("7e9035fcca8bfcbd616b2c0080297e6829d72b8c8f872d22729b039a319d68f041d147d5d920bbfa145a55424185ab1a1e3b01c3b9aba473")));
    }
}
